package com.fooddelivery.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fooddelivery.Common.Result;
import com.fooddelivery.Common.queryParam.QueryPageParamSaveTOrder;
import com.fooddelivery.Common.queryParam.QueryPageParamTOrder;
import com.fooddelivery.Common.responseVO.OrderDetailVO;
import com.fooddelivery.Entity.TOrder;
import com.fooddelivery.Service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author swy
 * @since 2025-09-09
 */
@RestController
@RequestMapping("/t-order")
public class TOrderController {
    @Autowired
    private TOrderService torderService;

    @RequestMapping("/saveOrder")
    public Result saveOrder(@RequestBody QueryPageParamSaveTOrder queryPageParamSaveTOrder) {
        torderService.saveOrder(queryPageParamSaveTOrder);
        return Result.suc();
    }


    @RequestMapping("/delete")
    public Result delete(@RequestBody TOrder tOrder) {
        torderService.removeById(tOrder.getId());
         return Result.suc("订单创建成功");
    }

    //获取本月所有订单信息
    @RequestMapping("/currentMthOrd")
    public Result currentMthOrd() {
        List<OrderDetailVO> orderDetailVOList = torderService.currentMthOrd();
        return Result.suc(orderDetailVOList);
    }


    //根据订单查询全部菜品信息
    @RequestMapping("order_detail")
    public Result order_detail(@RequestBody TOrder tOrder) {
        OrderDetailVO orderDetailVO = torderService.getOrderDetail(tOrder.getId());
        return Result.suc(orderDetailVO);
    }


    //根据买家ID查询订单以及订单的详情
    @RequestMapping("/person_orderList")
    public Result person_orderList(@RequestBody  TOrder tOrder) {
        List<OrderDetailVO> orderDetailVOList = torderService.person_orderList(tOrder.getUserId());
        //根据user_id的内容查找该人的所有订单
        return Result.suc(orderDetailVOList, Long.valueOf(orderDetailVOList.size()));
    }

    @RequestMapping("/cancel_order")
    public Result cancel_order(@RequestBody TOrder tOrder) {
        torderService.cancel_order(tOrder.getId());
        return Result.suc("取消成功");
    }

    @RequestMapping("/pay_order")
    public Result pay_order(@RequestBody TOrder tOrder) {
        torderService.payOrder(tOrder);
        return Result.suc("支付成功");
    }




    @PostMapping("/listPage")
    //@RequestBody 作用：将json数据映射为对象
    //或者直接用map接收
    //根据买家，手机号，支付状态，订单状态供查询
    public Result listPage(@RequestBody QueryPageParamTOrder  queryPageParamTOrder){
        Page<TOrder> page = new Page();
        page.setCurrent(queryPageParamTOrder.getPageNum());
        page.setSize(queryPageParamTOrder.getPageSize());

        LambdaQueryWrapper<TOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        TOrder torder = queryPageParamTOrder.getTorder();

        if (StringUtils.hasText(torder.getOrderNumber())){
            lambdaQueryWrapper.like(TOrder ::getOrderNumber,torder.getOrderNumber());
        }


        if (StringUtils.hasText(torder.getPhone())){
            lambdaQueryWrapper.eq(TOrder ::getPhone,torder.getPhone());
        }
        if (StringUtils.hasText(torder.getBuyer())){
            lambdaQueryWrapper.like(TOrder ::getBuyer,torder.getBuyer());
        }
        if (torder.getOrderState() != null){
            lambdaQueryWrapper.like(TOrder ::getOrderState,torder.getOrderState());
        }

        if (torder.getPayState() != null){
            lambdaQueryWrapper.like(TOrder ::getPayState,torder.getPayState());
        }
        IPage result=torderService.page(page,lambdaQueryWrapper);
        System.out.println("总查询数据"+result.getTotal());
        System.out.println("当前页数据"+result.getRecords().size());
        return Result.suc(result.getRecords(), result.getTotal());
    }

}
